﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Approval;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Approval
{
    public class DelLeaveApplicationCommendHandlers : IRequestHandler<DelLeaveApplicationCommends, APIResult<LeaveApplications>>
    {

        private readonly IBaseRepository<LeaveApplications> laRepository;

        public DelLeaveApplicationCommendHandlers(IBaseRepository<LeaveApplications> laRepository)
        {
            this.laRepository = laRepository;
        }

        /// <summary>
        /// 删除请假申请(逻辑删除)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<LeaveApplications>> Handle(DelLeaveApplicationCommends request, CancellationToken cancellationToken)
        {
            APIResult<LeaveApplications> result = new APIResult<LeaveApplications>();
    

            var las = laRepository.GetAllAsync().Result.FirstOrDefault(p=>p.ApplicationId==request.ApplicationId);
           if (las != null)
            {
                las.IsDelete=!las.IsDelete;
                await laRepository.Update(las);
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = las;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
